package org.androidpn.server.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.androidpn.server.util.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * entity 好友分组
 * 
 * @author 蔡琪
 * 
 */
@Entity
@Table(name = "tk_friend_group")
public class FriendGroup extends BaseEntity {

	private static final long serialVersionUID = -3691664246777581785L;

	/** 分组名称 */
	@Column(name = "group_name", length = 10)
	private String groupName;

	/**
	 * 是否为黑名单
	 */
	@Column(name = "isBlacklist")
	private Boolean isBlacklist = false;

	/** 所属角色 */
	@ManyToOne(fetch = FetchType.EAGER)
	private Role role;

	/** 分组下的好友 */
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "friendGroup")
	private Set<Friend> friends = new HashSet<Friend>();

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public Boolean getIsBlacklist() {
		return isBlacklist;
	}

	public void setIsBlacklist(Boolean isBlacklist) {
		this.isBlacklist = isBlacklist;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Set<Friend> getFriends() {
		return friends;
	}

	public void setFriends(Set<Friend> friends) {
		this.friends = friends;
	}

	/**
	 * 从xml字符串中解析出FriendGroup
	 * 
	 * @param xml
	 *            xml字符串
	 * @return Room对象
	 */
	@Transient
	public static FriendGroup fromXML(String xml) {
		FriendGroup friendGroup = BaseEntity.fromXML(xml, FriendGroup.class);
		Document doc;
		try {
			doc = DocumentHelper.parseText(xml);
			Element element = doc.getRootElement();
			// 组装Friend信息
			String groupName = element.elementText("groupName");
			String isBlacklist = element.elementText("isBlacklist");
			/** 角色信息 */
			Set<Friend> friends = friendGroup.getFriends();
			Element rolesElement = element.element("friends");
			if (rolesElement != null) {
				Iterator<Element> iterator = rolesElement
						.elementIterator("friend");
				while (iterator.hasNext()) {
					Friend friend = Friend.fromXML(iterator.next().asXML());
					friends.add(friend);
				}
			}
			friendGroup.setGroupName(groupName);
			friendGroup.setIsBlacklist(XMLUtils.getBooleanFromStr(isBlacklist));
			friendGroup.setFriends(friends);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return friendGroup;
	}

	/**
	 * 将当前对象转化为xml字符串
	 * 
	 * @return
	 */
	@Transient
	public String toXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<friendGroup>");
		buffer.append(super.toXML());
		buffer.append(XMLUtils.createSingleNode("groupName", this.groupName));
		buffer.append(XMLUtils
				.createSingleNode("isBlacklist", this.isBlacklist));
		if (!this.friends.isEmpty()) {
			buffer.append("<friends>");
			for (Friend friend : this.friends) {
				buffer.append(friend.toXML());
			}
			buffer.append("</friends>");
		}
		buffer.append("</friendGroup>");
		return buffer.toString();
	}
}
